
Imports Cdm
Imports Cdm.AppServices
Imports Cdm.AppServices.Log
Imports Cdm.DataCore.LookupTables

Partial Class Admin_appAffirmation
    Inherits System.Web.UI.Page

#Region "CONSTANTS"
    Private Const _CLASS_NAME As String = "Admin_appAffirmation"
#End Region

#Region "EVENT HANDLERS"

 


    Protected Sub AffirmationAddButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AffirmationAddButton.Click

        Try

            Me.AffirmationsScrollingGridView.Visible = False
            Me.AffirmationAddButton.Visible = False

            Me.SubmitButton.Visible = True
            Me.CancelButton.Visible = True
            Me.AffirmationsDetailsView.Visible = True

        Catch ex As Exception
            ErrorLog(_CLASS_NAME, "An error occurred in AffirmationAddButton_Click() method.", ex)
        End Try

    End Sub

    Protected Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton.Click

        Try

            Dim fieldName As TextBox = CType(Me.AffirmationsDetailsView.FindControl("AffirmationDetailsTextBox"), TextBox)
            Dim fieldActive As CheckBox = CType(Me.AffirmationsDetailsView.FindControl("ActiveCheckBox"), CheckBox)
            Dim sName As String = fieldName.Text.Trim()

            'VALIDATE DATA
            If sName = "" Then
                'Me.ErrorSummary.SetSingleError("You need to enter a value for Affirmation before continuing.")
                'Me.ErrorSummary.Visible = True
                SetStatus("You need to enter a value for Affirmation before continuing.")
                Exit Sub
            End If

            'CHECK IF THE NAME ALREADY EXISTS IN THE DATABASE
            If LuAffirmation.Exists(0, sName) Then
                'Me.ErrorSummary.SetSingleError("An Affirmation by that name already exists. Please enter a different name.")
                'Me.ErrorSummary.Visible = True
                SetStatus("An Affirmation by that name already exists. Please enter a different name.")
                Exit Sub
            End If

            'HIDE THE INPUT FORM
            Me.SubmitButton.Visible = False
            Me.CancelButton.Visible = False
            Me.AffirmationsDetailsView.Visible = False

            'ADD THE NEW AFFIRAMTION
            Dim oAffirmation As New LuAffirmation()
            oAffirmation.Description = sName
            oAffirmation.IsActive = fieldActive.Checked
            oAffirmation.Persist()

            'RETRIEVE A NEW DATASET
            Me.AffirmationsObjectDataSource.Select()
            Me.AffirmationsScrollingGridView.DataBind()

            'CLEAR THE IPUT FORM
            fieldName.Text = String.Empty

            'CLEAR THE ERROR MESSAGE
            If Me.ErrorSummary.Visible Then
                Me.ErrorSummary.Visible = False
            End If

            'DISPLAY THE GRIDVIEW
            Me.AffirmationsScrollingGridView.Visible = True
            Me.AffirmationAddButton.Visible = True
            SetStatus("New Affirmation added successfully.")


        Catch ex As Exception
            ErrorLog(_CLASS_NAME, "An error occurred in AffirmationAddButton_Click() method.", ex)
            SetStatus("Error Adding Affirmation - " + ex.Message)
        End Try

    End Sub

    Protected Sub CancelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CancelButton.Click

        Try

            Me.SubmitButton.Visible = False
            Me.CancelButton.Visible = False
            Me.AffirmationsDetailsView.Visible = False
            Me.ErrorSummary.Visible = False

            Me.AffirmationsScrollingGridView.Visible = True
            Me.AffirmationAddButton.Visible = True

            SetStatus("Action cancelled.")

        Catch ex As Exception
            ErrorLog(_CLASS_NAME, "An error occurred in CancelButton_Click() method.", ex)
        End Try

    End Sub

    Protected Sub AffirmationsDetailsView_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles AffirmationsDetailsView.ItemInserting

        Try

            Me.AffirmationsDetailsView.Visible = False

        Catch ex As Exception
            ErrorLog(_CLASS_NAME, "An error occurred in AffirmationsDetailsView_ItemInserting() method.", ex)
        End Try

    End Sub

#End Region

    Protected Sub AffirmationsScrollingGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles AffirmationsScrollingGridView.RowCancelingEdit
        Try

            If Me.ErrorSummary.Visible = True Then
                Me.ErrorSummary.Visible = False
                Me.AffirmationsScrollingGridView.Visible = True
            End If

            SetStatus("Update Cancelled.")

        Catch ex As Exception
            ErrorLog(_CLASS_NAME, "An error occurred in AffirmationsScrollingGridview_RowCancelingEdit() method.", ex)
        End Try

    End Sub

    Protected Sub AffirmationsScrollingGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles AffirmationsScrollingGridView.RowDataBound

        Try
            If e.Row.DataItemIndex = 0 Then
                AccessibilityController.AddImageToSortableGridHeaders(AffirmationsScrollingGridView)
            End If
            Dim fieldName As TextBox = CType(e.Row.FindControl("AffirmationTextBox"), TextBox)
            If Not fieldName Is Nothing Then
                SetFocus(fieldName.ClientID)
            End If

            Dim activeCheckBox As CheckBox = e.Row.FindControl("ViewActiveCheckBox")
            If Not activeCheckBox Is Nothing Then
                activeCheckBox.InputAttributes.Add("title", "check if Agency " & CType(e.Row.Cells(0).Controls(0), DataBoundLiteralControl).Text & " is active.")
            End If
            activeCheckBox = e.Row.FindControl("EditActiveCheckBox")
            If Not activeCheckBox Is Nothing Then
                activeCheckBox.InputAttributes.Add("title", "check if Agency " & CType(e.Row.Cells(0).Controls(1), TextBox).Text & " is active.")
            End If

        Catch ex As Exception
            ErrorLog(_CLASS_NAME, "An error occurred in AffirmationsScrollingGridview_RowDataBound() method.", ex)
        End Try

    End Sub

    Protected Sub AffirmationsScrollingGridView_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles AffirmationsScrollingGridView.RowUpdated
        SetStatus("Item Updated.")
    End Sub

    Protected Sub AffirmationsScrollingGridView_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles AffirmationsScrollingGridView.RowUpdating
        Dim strNV As String
        Dim sValue As String
        Dim iIdx As Integer
        Dim strOV As String
        Dim iAffirmationId As Integer = CType(e.Keys(0), Integer)

        Try

            If e.OldValues.Item(iIdx) IsNot Nothing Then
                strOV = e.OldValues.Item(iIdx).ToString.ToUpper
            Else
                strOV = ""
            End If

            If e.NewValues.Item(iIdx) IsNot Nothing Then
                strNV = e.NewValues.Item(iIdx).ToString.ToUpper
                If strOV = strNV Then
                    If Me.ErrorSummary.Visible = True Then
                        Me.ErrorSummary.Visible = False
                    End If
                End If

                'CHECK IF AN AFFIRMATION RECORD BY THIS NAME ALREADY EXISTS
                If LuAffirmation.Exists(iAffirmationId, strNV) Then
                    sValue = "An Affirmation by that name already exists. Please enter a different name."
                    'Me.ErrorSummary.SetSingleError(sValue)
                    'Me.ErrorSummary.Visible = True
                    e.Cancel = True
                    SetStatus(sValue)
                    Exit Sub
                End If

                'Passed error checking now update database
                Me.ErrorSummary.Visible = False
            Else
                sValue = "You must enter a value in the field before updating."
                'Me.ErrorSummary.SetSingleError(sValue)
                'Me.ErrorSummary.Visible = True
                e.Cancel = True
                SetStatus(sValue)
            End If

        Catch ex As Exception
            ErrorLog(_CLASS_NAME, "An error occurred in AffirmationsScrollingGridview_RowUpdating() method.", ex)
            SetStatus("Error in Updating -- " + ex.Message)
        End Try

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetupJavaScript()
        ClearStatus()
    End Sub

    Private Sub SetupJavaScript()

        EntityController.JavaScriptAddChildDiv(Me.AffirmationsScrollingGridView.DivClientId, CInt(Me.AffirmationsScrollingGridView.ToolTip))
        ClientScript.RegisterStartupScript(Me.GetType(), "scrollDivs", EntityController.JavaScriptScrollingGridStartup(Me.AffirmationsScrollingGridView), True)
        EntityController.JavaScriptSetFixedHeight(30)
    End Sub

    Private Sub ClearStatus()
        SetStatus("")
    End Sub
    Private Sub SetStatus(ByVal message As String)
        Dim mymaster As ASP.admin_admin_master = CType(Me.Master, ASP.admin_admin_master)
        mymaster.SetStatus(message)
    End Sub
End Class
